package top.malaoshi.netdisk.ctrl;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.malaoshi.netdisk.service.LoginSrvImpl;
import top.malaoshi.netdisk.vo.JsonResult;
import top.malaoshi.netdisk.vo.User;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@RestController
@Validated
public class LoginCtrl {

    @Resource
    private LoginSrvImpl loginSrv;
    @PostMapping("/login")
//    public JsonResult login(HttpSession session, String username, String password,HttpServletRequest request) throws Exception {
    public JsonResult login(HttpSession session,@Valid @RequestBody User user) throws Exception {

        User userResult = null;

        userResult = loginSrv.login(user);
//            session.setAttribute(userResult.getId()+"",userResult);
        return JsonResult.success("登录成功！",userResult);

    }

//    @ExceptionHandler(Exception.class)
    public JsonResult exception(HttpSession session,RuntimeException e){
        JsonResult jr = JsonResult.fail(2000, e.getMessage());
        return jr;
    }
}
